package services

import (
	"fmt"
	"github.com/gin-gonic/gin"
	. "yunwei/go_api/application/model"
	. "yunwei/go_api/application/utils"
	"yunwei/go_api/application/validator"
)

/**
添加主机类别
*/

func CreateHostCategory(ctx *gin.Context) (HostCategoryInstance, error) {
	hostCategory := HostCategory{}
	var instance HostCategoryInstance
	var err error
	if err = ctx.ShouldBindJSON(&hostCategory); err != nil {
		return instance, err
	}

	if err = ValidateData(hostCategory); err != nil {
		return instance, err
	}
	err = hostCategory.Insert()
	instance = HostCategoryInstance{
		ID:   hostCategory.ID,
		Name: hostCategory.Name,
	}
	return instance, err
}

/*
*
获取主机类别列表
*/
func GetHostCategoryList(ctx *gin.Context) ([]HostCategoryInstance, error) {
	hostCategory := HostCategory{}
	hostCategoryList, err := hostCategory.GetAll()
	return hostCategoryList, err
}

/**
添加主机
*/

func CreateHost(ctx *gin.Context) (HostInstance, error) {
	host := Host{}
	var err error
	var instance HostInstance
	var hostCategory HostCategory
	if err = ctx.ShouldBind(&host); err != nil {
		fmt.Println("host::", host)
		fmt.Println("err::", err)
		return instance, err
	}
	if err = validator.HostValidator(&host, ctx); err != nil {
		return instance, err
	}
	err = host.Insert()
	if err != nil {
		return instance, err
	}
	// (4) 序列化
	//fmt.Println("插入数据库成功后的host:::", host)
	err = hostCategory.GetOneById(host.HostCategoryID)
	if err != nil {
		return instance, err
	}

	instance = HostInstance{
		ID:           host.ID,
		Name:         host.Name,
		IpAddr:       host.IpAddr,
		Port:         host.Port,
		Username:     host.Username,
		Remark:       host.Remark,
		CategoryID:   host.HostCategoryID,
		CategoryName: hostCategory.Name,
	}
	return instance, err
}

/**
获取主机列表信息
*/

func GetHostList(name string, hostCategoryId uint, hostname string) ([]HostInstance, error) {
	var hostList []Host
	var instanceList []HostInstance
	var host Host
	var err error
	hostList, err = host.GetAll(name, uint(hostCategoryId), hostname)
	for _, hostItem := range hostList {
		instanceList = append(instanceList, HostInstance{
			ID:           hostItem.ID,
			Name:         hostItem.Name,
			IpAddr:       hostItem.IpAddr,
			Port:         hostItem.Port,
			Username:     hostItem.Username,
			CategoryID:   hostItem.HostCategory.ID,
			CategoryName: hostItem.HostCategory.Name,
			Remark:       hostItem.Remark,
		})
	}
	return instanceList, err
}

/**
删除主机列表信息
*/

func DeleteHost(hostId uint) (HostInstance, error) {
	var delHost Host
	var instance HostInstance

	err := delHost.GetOneById(hostId)
	if err != nil {
		return instance, err
	}
	err = delHost.Delete()
	if err != nil {
		return instance, err
	}

	instance = HostInstance{
		Name:       delHost.Name,
		IpAddr:     delHost.IpAddr,
		Port:       delHost.Port,
		Username:   delHost.Username,
		Remark:     delHost.Remark,
		CategoryID: delHost.HostCategoryID,
	}

	return instance, err
}
